package com.kgc.ljp.WriterDemo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


    public class DeepcopyDemo {
        public static Object deepCope(Object obj){
            try{
                //1. 对象序列化
                // 缓冲流: 字节数组输出流
                ByteArrayOutputStream buf =
                        new ByteArrayOutputStream();
                //对象输出流
                ObjectOutputStream out =
                        new ObjectOutputStream(
                                new ByteArrayOutputStream());

                out.writeObject(obj);//序列化对象到buf中
                out.close();

                //2 .对象的反序列化
                byte[] ary = buf.toByteArray();
                ByteArrayInputStream bais =
                        new ByteArrayInputStream(ary);
                ObjectInputStream in =
                        new ObjectInputStream(bais);
                Object o = in.readObject();//从ary反序列化
                in.close();
                return o;

            }catch(Exception e){
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
